package com.googlecode.mylyn.ui.wizard;

import org.apache.commons.lang.StringUtils;
import org.eclipse.mylyn.internal.provisional.tasks.ui.wizards.AbstractRepositoryQueryPage2;
import org.eclipse.mylyn.tasks.core.IRepositoryQuery;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.googlecode.mylyn.core.util.QueryUtils;

/**
 * A wizard page that allows you to define a query for Google code tasks.
 */
@SuppressWarnings("restriction")
public class GoogleCodeDefinitionQueryPage extends AbstractRepositoryQueryPage2 {

    private Text fullTextQuery;
    private Text labelText;
    private Text ownerText;
    private Text statusText;

    /**
     * Initializes a new {@link GoogleCodeDefinitionQueryPage}.
     *
     * @param repository the repository on which the query should operate
     * @param query the query to edit
     */
    public GoogleCodeDefinitionQueryPage(TaskRepository repository, IRepositoryQuery query) {
        super("googleCodeFilterDefinition", repository, query);
    }

    @Override
    protected void createPageContent(Composite parent) {
        Composite composite = new Composite(parent, SWT.BORDER);
        composite.setLayout(new GridLayout(2, false));

        Label label = new Label(composite, SWT.NONE);
        label.setText("Containing text:"); //TODO translate
        label.setLayoutData(getFullWidthGridData());

        this.fullTextQuery = new Text(composite, SWT.SINGLE | SWT.BORDER);
        this.fullTextQuery.setLayoutData(getFullWidthGridData());
        this.fullTextQuery.setToolTipText("Full-text query string"); //TODO translate

        label = new Label(composite, SWT.NONE);
        label.setText("Having attributes:"); //TODO translate
        label.setLayoutData(getFullWidthGridData());

        label = new Label(composite, SWT.NONE);
        label.setText("Label:"); //TODO translate
        this.labelText = new Text(composite, SWT.SINGLE | SWT.BORDER);
        this.labelText.setLayoutData(this.getTextGridData());

        label = new Label(composite, SWT.NONE);
        label.setText("Owner:"); //TODO translate
        this.ownerText = new Text(composite, SWT.SINGLE | SWT.BORDER);
        this.ownerText.setLayoutData(this.getTextGridData());

        label = new Label(composite, SWT.NONE);
        label.setText("Status:"); //TODO translate
        this.statusText = new Text(composite, SWT.SINGLE | SWT.BORDER);
        this.statusText.setLayoutData(this.getTextGridData());
    }

    private GridData getFullWidthGridData() {
        GridData layoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
        layoutData.horizontalSpan = 2;
        return layoutData;
    }

    private GridData getTextGridData() {
        return new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    }

    @Override
    protected void doRefresh() {
        // TODO Auto-generated method stub
    }

    @Override
    protected boolean hasRepositoryConfiguration() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected boolean restoreState(IRepositoryQuery query) {
        if (query != null) {
            this.fullTextQuery.setText(StringUtils.defaultString(query.getAttribute(QueryUtils.LABEL)));
            this.labelText.setText(StringUtils.defaultString(query.getAttribute(QueryUtils.LABEL)));
            this.ownerText.setText(StringUtils.defaultString(query.getAttribute(QueryUtils.OWNER)));
            this.statusText.setText(StringUtils.defaultString(query.getAttribute(QueryUtils.STATUS)));
        } else {
            this.fullTextQuery.setText(StringUtils.EMPTY);
            this.labelText.setText(StringUtils.EMPTY);
            this.ownerText.setText(StringUtils.EMPTY);
            this.statusText.setText(StringUtils.EMPTY);
        }
        return true;
    }

    @Override
    public void applyTo(IRepositoryQuery query) {
        String queryTitle = this.getQueryTitle();
        if (queryTitle != null) {
            query.setSummary(queryTitle);
        }

        //remove old attributes in case of editing
        query.getAttributes().clear();
        if (this.fullTextQuery.getText().length() > 0) {
            query.setAttribute(QueryUtils.Q, this.fullTextQuery.getText());
        }
        if (this.labelText.getText().length() > 0) {
            query.setAttribute(QueryUtils.LABEL, this.labelText.getText());
        }
        if (this.ownerText.getText().length() > 0) {
            query.setAttribute(QueryUtils.OWNER, this.ownerText.getText());
        }
        if (this.statusText.getText().length() > 0) {
            query.setAttribute(QueryUtils.STATUS, this.statusText.getText());
        }
    }

}
